package com.shchuangzhi.smartguard.core;

import com.elvishew.xlog.XLog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by j_hu on 5/18/17.
 */

public class InOutRecord {
    private int id;
    private int no;
    private int seq;
    private int state;
    private int machine;
    private String time;
    private String name;
    private String className;
    private String stateName;
    private boolean isManual;

    private final static int DUP_MINUTES = 30;
    private final static int STATE_CHANGE_SECONDS = 5;

    public InOutRecord(
            int id,
            int machine,
            int no,
            int seq,
            int state,
            boolean isManual,
            String name,
            String stateName,
            String className,
            String time) {
        this.id = id;
        this.machine = machine;
        this.no = no;
        this.seq = seq;
        this.name = name;
        this.time = time;
        this.state = state;
        this.isManual = isManual;
        this.className = className;
        this.stateName = stateName;
    }

    public int getId() {
        return id;
    }

    public int getMachine() {
        return machine;
    }

    public int getNo() {
        return no;
    }

    public int getSeq() { return seq; }

    public int getState() {
        return state;
    }

    public String getName() { return name; }

    public boolean isManual() { return isManual; }

    public String getTimeStr() { return time; }

    public String getShortTimeStr() {
        String timeStr = "";
        Date recTime = getTime();
        if (recTime != null) {
            timeStr = new SimpleDateFormat("a HH:mm").format(recTime);
        }
        return timeStr;
    }

    public Date getTime(){
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(time);
        } catch(Exception e) {
            XLog.e("", e);
        }
        return date;
    }

    public boolean isStateDup(InOutRecord record) {
        Calendar right = Calendar.getInstance();
        right.setTime(getTime());
        right.add(Calendar.MINUTE, DUP_MINUTES);
        return right.getTime().after(record.getTime());
    }

    public boolean canStateChange(InOutRecord record) {
        boolean canStateChange = true;
        if (record.getState() != Consts.STATE_HEALTH) {
            Calendar right = Calendar.getInstance();
            right.setTime(getTime());
            right.add(Calendar.SECOND, STATE_CHANGE_SECONDS);
            canStateChange = right.getTime().after(record.getTime());
        }
        return canStateChange;
    }

    public String getClassName() { return className; }

    public String getStateName() { return stateName; }
}
